#########################
#   Author : Lion yu    #
#   Date : 2023/06/07   #
#   Id : SM2772         #
#########################
import os
import datetime
from datetime import date, timedelta, datetime as dt
import pandas as pd
from handle import Handle, ProjectConfig
# from handle import ProjectBugsLevel_Enumeration

# 统计缺陷方法
def statisticsData(projectId):
    bugSnapshot_Path = Handle.bugSnapshotData_path
    bug_Path = Handle.bugData_path
    bugSnapshot_filePath = os.path.join(bugSnapshot_Path, f"{projectId}.pkl")
    bug_filePath = os.path.join(bug_Path, f"{projectId}.pkl")
    statisticsdata_path = Handle.statistics2Month_path
    statisticsFile = os.path.join(statisticsdata_path, f"{projectId}.pkl")
    
    bugs_snapshot:pd.DataFrame = pd.read_pickle(bugSnapshot_filePath)
    bugs:pd.DataFrame = pd.read_pickle(bug_filePath)
    
    bugs_snapshot.drop_duplicates(subset=['projectId', 'bugId'], keep='last', inplace=True)
    bugs_snapshot.rename(columns={'bugId':'taskId'}, inplace=True)
    bugs_snapshot['tfsCloseTime'] = bugs_snapshot['tfsCloseTime'].apply(lambda x:dt.strptime(x, '%Y-%m-%d %H:%M:%S'))
    bugs_snapshot['tfsCreateTime'] = bugs_snapshot['tfsCreateTime'].apply(lambda x:dt.strptime(x, '%Y-%m-%d %H:%M:%S'))
    
    min_time = str(bugs_snapshot['tfsCreateTime'].min())[:10]
    min_time_split = min_time.split('-')
    min_time_int = list(map(int, min_time_split))
    min_time = datetime.date(*min_time_int)
    max_time = (date.today() + timedelta(days=31)).strftime("%Y-%m-%d %H:%M:%S")[:10]
    max_time = datetime.date(*map(int, max_time.split('-')))
    start_date = min_time  # 根据数据源获取最早日期 yyyy-mm-dd
    delta = datetime.timedelta(days=1)
    date_range = []
    No_range = []
    No = 0
    while (start_date <= max_time):
        date_range.append(str(start_date))
        start_date += delta
        No += 1
        No_range.append(No)
    data_for_FJ = {
            'No' : No_range,
            'Month' : date_range
            }
    # 获取项目下的缺陷分类枚举
    bugTypes = list(set(bugs['bugType_name']))
    df_template = pd.DataFrame(data_for_FJ)
    df_template['Month'] = df_template['Month'].apply(lambda x : x[:7])
    df_template = df_template.groupby('Month').apply(lambda x : ','.join(x))
    df_template = df_template.reset_index()
    df_template = df_template[['Month']]
    df_template['Month_ext'] = df_template['Month'].apply(lambda x:int(x.replace('-','')))
    df_template_tmpL = []
    for i in bugTypes:
        for row in df_template.itertuples():
            row_dict = {
                "Month" : getattr(row, 'Month'),
                "Month_ext" : getattr(row, 'Month_ext'),
                "bugType_name" : i
            }
            df_template_tmpL.append(row_dict)
            
    df_template = pd.DataFrame(df_template_tmpL)
    df_template['bugType_name'] = df_template['bugType_name'].apply(lambda x:"无" if len(x) == 0 else x)
    base_data = pd.merge(bugs, bugs_snapshot, how='inner', on='taskId', suffixes=("_bugs", "_bugsSnapshot"))
    base_data['CreateMonth'] = base_data['tfsCreateTime'].apply(lambda x:int(str(x)[:7].replace('-','')))
    base_data['CloseMonth'] = base_data['tfsCloseTime'].apply(lambda x:int(str(x)[:7].replace('-','')))
    base_data['bugType_name'] = base_data['bugType_name'].apply(lambda x:"无" if len(x) == 0 else x)
    
    A_bug:pd.DataFrame = base_data[base_data['customfields_title'] == ProjectConfig[projectId]['ProjectBugsLevel_Enumeration']['buglevel_A']]
    B_bug:pd.DataFrame = base_data[base_data['customfields_title'] == ProjectConfig[projectId]['ProjectBugsLevel_Enumeration']['buglevel_B']]
    C_bug:pd.DataFrame = base_data[base_data['customfields_title'] == ProjectConfig[projectId]['ProjectBugsLevel_Enumeration']['buglevel_C']]
    D_bug:pd.DataFrame = base_data[base_data['customfields_title'] == ProjectConfig[projectId]['ProjectBugsLevel_Enumeration']['buglevel_D']]
    # 过滤掉创建月份 == 关闭月份 的数据
    LegacyBugs_df = base_data[base_data['CloseMonth'] > base_data['CreateMonth']]
    del base_data
    
    New_A = A_bug.groupby(['CreateMonth', 'bugType_name']).count()['tfsCreateTime']
    New_A = pd.DataFrame(New_A)
    New_A.reset_index(inplace=True)
    New_A.rename(columns={'tfsCreateTime' : "Create_A", 'CreateMonth' : 'Month_ext'}, inplace=True)
    
    New_B = B_bug.groupby(['CreateMonth', 'bugType_name']).count()['tfsCreateTime']
    New_B = pd.DataFrame(New_B)
    New_B.reset_index(inplace=True)
    New_B.rename(columns={'tfsCreateTime' : "Create_B", 'CreateMonth' : 'Month_ext'}, inplace=True)
    
    New_C = C_bug.groupby(['CreateMonth', 'bugType_name']).count()['tfsCreateTime']
    New_C = pd.DataFrame(New_C)
    New_C.reset_index(inplace=True)
    New_C.rename(columns={'tfsCreateTime' : "Create_C", 'CreateMonth' : 'Month_ext'}, inplace=True)
    
    New_D = D_bug.groupby(['CreateMonth', 'bugType_name']).count()['tfsCreateTime']
    New_D = pd.DataFrame(New_D)
    New_D.reset_index(inplace=True)
    New_D.rename(columns={'tfsCreateTime' : "Create_D", 'CreateMonth' : 'Month_ext'}, inplace=True)
    
    Close_A = A_bug.groupby(['CloseMonth', 'bugType_name']).count()['tfsCreateTime']
    Close_A = pd.DataFrame(Close_A)
    Close_A.reset_index(inplace=True)
    Close_A.rename(columns={'tfsCreateTime' : "Close_A", 'CloseMonth' : 'Month_ext'}, inplace=True)
    
    Close_B = B_bug.groupby(['CloseMonth', 'bugType_name']).count()['tfsCreateTime']
    Close_B = pd.DataFrame(Close_B)
    Close_B.reset_index(inplace=True)
    Close_B.rename(columns={'tfsCreateTime' : "Close_B", 'CloseMonth' : 'Month_ext'}, inplace=True)
    
    Close_C = C_bug.groupby(['CloseMonth', 'bugType_name']).count()['tfsCreateTime']
    Close_C = pd.DataFrame(Close_C)
    Close_C.reset_index(inplace=True)
    Close_C.rename(columns={'tfsCreateTime' : "Close_C", 'CloseMonth' : 'Month_ext'}, inplace=True)
    
    Close_D = D_bug.groupby(['CloseMonth', 'bugType_name']).count()['tfsCreateTime']
    Close_D = pd.DataFrame(Close_D)
    Close_D.reset_index(inplace=True)
    Close_D.rename(columns={'tfsCreateTime' : "Close_D", 'CloseMonth' : 'Month_ext'}, inplace=True)
    
    bugdata_ext = [New_A, New_B, New_C, New_D, Close_A, Close_B, Close_C, Close_D]
    del A_bug, B_bug, C_bug, D_bug
    del New_A, New_B, New_C, New_D, Close_A, Close_B, Close_C, Close_D
    suffixes_num = 0
    for i in bugdata_ext:
        df_template = pd.merge(df_template, i, how='left', on=['Month_ext', 'bugType_name'], suffixes=(f"_{suffixes_num}", f"_{suffixes_num+1}"))
        df_template.fillna(0, inplace=True)
        df_column1 = i.columns[-1]
        df_template[df_column1] = df_template[df_column1].astype('int64')
        suffixes_num += 1
    # 计算遗留bug
    df_template['Legacy_A'] = 0
    df_template['Legacy_B'] = 0
    df_template['Legacy_C'] = 0
    df_template['Legacy_D'] = 0
    
    bugState_map = {
        "buglevel_A" : "Legacy_A",
        "buglevel_B" : "Legacy_B",
        "buglevel_C" : "Legacy_C",
        "buglevel_D" : "Legacy_D",
    }
    
    bugState_map_ext = ProjectConfig[projectId]['ProjectBugsLevel_Enumeration']
    bugState_map_ext = {j:i for i, j in bugState_map_ext .items()}
    
    bugState_map_ext['一般'] = 'buglevel_C'    # 此行代码为了兼容大程序存在两个同样字段且同样缺陷等级id的奇葩数据！
    bugState_map_ext['严重'] = 'buglevel_B'
    
    for row in df_template.itertuples():
        Month_ext = getattr(row, 'Month_ext')
        df_template_bugTypeName = getattr(row, 'bugType_name')
        rowIndex = getattr(row, 'Index')
        for LegacyBugs_row in LegacyBugs_df.itertuples():
            bugl = getattr(LegacyBugs_row, "customfields_title")
            created_Month = int(getattr(LegacyBugs_row, "CreateMonth"))
            finished_Month = int(getattr(LegacyBugs_row, "CloseMonth"))
            LegacyBugs_df_bugTypeName = getattr(LegacyBugs_row, "bugType_name")
            if Month_ext >= created_Month and Month_ext < finished_Month and df_template_bugTypeName == LegacyBugs_df_bugTypeName:
                df_template.at[rowIndex, bugState_map[bugState_map_ext[bugl]]] += 1
                continue
    del LegacyBugs_df
        
    df_template['Month_Legacy_DI'] = (df_template['Legacy_A']*10 + df_template['Legacy_B']*3 + df_template['Legacy_C'] + df_template['Legacy_D']*0.3)
    df_template['Month_Create_DI'] = (df_template['Create_A']*10 + df_template['Create_B']*3 + df_template['Create_C'] + df_template['Create_D']*0.3)
    df_template['Month_Close_DI'] = (df_template['Close_A']*10 + df_template['Close_B']*3 + df_template['Close_C'] + df_template['Close_D']*0.3)
    df_template.drop(labels='Month_ext', axis=1, inplace=True)
    df_template['projectId'] = projectId
    df_template.to_pickle(statisticsFile)
    return